﻿<template>
  <div class="app-container">
    <div class="chart-title">档案管理</div>
    <div class="chart-content">
      <div class="filter-title">
        <el-form label-position="right">
          <el-form-item>
            <el-input
              v-model="listQuery.mobile"
              style="width: 160px; height:32px;"
              class="filter-item"
              :placeholder="$t('member.mobile')"
              @keyup.enter.native="handleFilter"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="listQuery.nickname"
              style="width: 160px; height:32px;"
              class="filter-item"
              :placeholder="$t('member.nickname')"
              @keyup.enter.native="handleFilter"
            ></el-input>
          </el-form-item>
          <div
            class="search-button"
            @click="handleFilter"
          >
            <img src="@/assets/images/bg_search@2x.png" />
          </div>
          <div
            class="search-button"
            @click="handleRefresh"
          >
            <img src="@/assets/images/bg_reset@2x.png" />
          </div>
        </el-form>
      </div>
      <div class="chart-list">
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255,255,255,0.2)"
          border
          fit
          highlight-current-row
          style="width: 100%min-height:1000px"
        >
          <el-table-column
            fixed="left"
            align="center"
            :label="$t('table.id')"
            width="65"
          >
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.nickname')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.nickname }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.headimg')"
          >
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.headimg"
                fit="contain"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.mobile')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.mobile }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('public.creationTime')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.create_time }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.sumMoney')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.sum_money }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.buyCount')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.buy_count }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('member.finally')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.finally_buy_time }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('table.actions')"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="operat-button">
                <div
                  class="read-img"
                  @click="handleRead(scope.row)"
                >
                  <img src="@/assets/images/read@2x.png" />
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
      <!-- 编辑 -->
      <el-dialog
        :title="textMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
      >
        <el-form
          ref="dataForm"
          :model="temp"
          label-position="right"
          label-width="35%"
          style="width: 400px margin-left:120px"
          size="small"
        >
          <div class="popup-form">
            <div class="header-form">
              <div class="from-item">
                <el-table :data="temp.goods_list">
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    :label="$t('order.time')"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.create_time }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    :label="$t('meal.name')"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.goods_name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    :label="$t('public.number')"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.number }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    :label="$t('order.price')"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.sum_price }}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </el-form>
        <div
          slot="footer"
          class="dialog-footer"
        >
          <div>
            <el-button @click="dialogFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>
            <el-button
              type="primary"
              @click="dialogFormVisible = false"
            >{{
              $t("table.confirm")
            }}</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script type="text/javascript">
import { getToken } from "@/utils/auth";
export default {
  name: "Itoc",
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        mobile: "",
        nickname: ""
      },
      temp: {
        id: undefined,
        goods_list: []
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      }
    };
  },
  created() {
    const token = getToken();
    if (
      token &&
      !this.$store.state.socket.loginTransition &&
      !this.$store.state.socket.loginAccount
    ) {
      this.tokenLogin(token);
    }
    this.getList();
  },
  methods: {
    tokenLogin(token) {
      const path = {
        app: "wxadmin",
        api: "api_wxadmin_admin_keepLogin",
        version: "1.0",
        type: 2,
        data: {
          token: token
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        this.socketData(res);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        category_name: "",
        partner_name: "",
        merchant_name: "",
        invest_name: ""
      };
      this.getList();
    },
    // 获取会员列表
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        mobile: this.listQuery.mobile ? this.listQuery.mobile : "",
        nickname: this.listQuery.nickname ? this.listQuery.nickname : ""
      };
      const path = {
        app: "depot",
        api: "api_member_index_getList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 打开查看弹窗
    handleRead(row) {
      this.dialogStatus = "read";
      const path = {
        app: "member",
        api: "api_member_index_getDetails",
        version: "1.0",
        type: 2,
        data: {
          id: row.id
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            });
          } else if (resj.api === "api_member_index_getList") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "api_member_index_getDetails") {
            this.temp.goods_list = resj.data;
            this.dialogFormVisible = true;
          }
        }
      }
    }
  }
};
</script>
